package com.asset.salesforce.controller;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.asset.salesforce.domain.Account;
import com.asset.salesforce.domain.Employee;
import com.asset.salesforce.domain.FollowCenter;
import com.asset.salesforce.domain.FollowLead;
//import com.asset.salesforce.domain.FollowType;
import com.asset.salesforce.domain.Lead;
import com.asset.salesforce.domain.Post;
import com.asset.salesforce.domain.UpdateChatter;
import com.asset.salesforce.enums.FOLLOWTYPE;
import com.asset.salesforce.enums.UPDATETYPE;
//import com.asset.salesforce.domain.UpdateType;
import com.asset.salesforce.service.AccountService;
import com.asset.salesforce.service.ContactService;
import com.asset.salesforce.service.EmployeeService;
import com.asset.salesforce.service.FollowCenterService;
import com.asset.salesforce.service.FollowLeadService;
//import com.asset.salesforce.service.FollowTypeService;
import com.asset.salesforce.service.LeadService;
import com.asset.salesforce.service.PostService;
import com.asset.salesforce.service.UpdateChatterService;
//import com.asset.salesforce.service.UpdateTypeService;


@Controller
public class FollowController {
	
	@Autowired
	private FollowLeadService followLeadService;
	
//	@Autowired 
//	private FollowTypeService followTypeService;
	
	@Autowired
	private FollowCenterService followCenterService;
	
	@Autowired
	
	private LeadService leadService;
	
	@Autowired
	private UpdateChatterService updateChatterService;
	
//	@Autowired 
//	private UpdateTypeService updateTypeService;
	@Autowired 
	private PostService postService;
	
	@Autowired
	private AccountService accountService;
	
	@Autowired
	private EmployeeService employeeService;
	@Autowired
	private ContactService contactService;
	@RequestMapping(value="/followLead", method=RequestMethod.GET)
	public String getFollow(Model model,HttpServletRequest request,HttpServletResponse response,HttpSession session){
		if (SecurityUtils.getSubject().hasRole("leadFellow")){
		Employee userDetails = (Employee)session.getAttribute("userDetails");
		model.addAttribute("userDetails",userDetails);
		Integer leadId= Integer.parseInt(request.getParameter("leadId").toString());
//		FollowType followType = followTypeService.getFollowTypeById(3);
		FollowCenter followCenter = new FollowCenter();
		followCenter.setDate(new Date());
		followCenter.setFollowedType(FOLLOWTYPE.Lead.getType());
		followCenter.setFollower(userDetails);
		followCenter.setFollowedId(leadId);
		followCenterService.addfollowCenter(followCenter);
		//save post to view on chatter page
//		UpdateType updateType = updateTypeService.getById(3);
		UpdateChatter updateChatter = new UpdateChatter();
		updateChatter.setAction_emp(userDetails);
		updateChatter.setDate(new Date());
		updateChatter.setFollowedId(leadId);
		updateChatter.setFollowType(FOLLOWTYPE.Lead.getType());
		updateChatter.setUpdateType(UPDATETYPE.Followed.getName());
		
		Post post = new Post();
		post.setPostDate(new Date());
		post.setPoster(userDetails);
		post.setPostText("<a href = '/sales/s/viewLead.html?id="+leadId+ "&b=c'>i followed Lead Name:"+ leadService.getLeadById(leadId).getfName() + " "+leadService.getLeadById(leadId).getfName()+"</a>");
		postService.addPost(post);	
		updateChatter.setPost(post);
		updateChatterService.add(updateChatter);
		
		return "redirect:/s/leads.html";
		}else{
			return "accessDenied";
		}
	}
	
	@RequestMapping(value="/followAccount", method=RequestMethod.GET)
	public String getFollowAccount(Model model,HttpServletRequest request,HttpServletResponse response,HttpSession session){
		if (SecurityUtils.getSubject().hasRole("accountFellow")){
		Employee userDetails = (Employee)session.getAttribute("userDetails");
		Integer accountId= Integer.parseInt(request.getParameter("accountId").toString());
//		FollowType followType = followTypeService.getFollowTypeById(1);
		FollowCenter followCenter = new FollowCenter();
		followCenter.setDate(new Date());
		followCenter.setFollowedType(FOLLOWTYPE.Account.getType());
		followCenter.setFollower(userDetails);
		followCenter.setFollowedId(accountId);
		followCenterService.addfollowCenter(followCenter);
		//save post to view on chatter page
//		UpdateType updateType = updateTypeService.getById(3);
		UpdateChatter updateChatter = new UpdateChatter();
		updateChatter.setAction_emp(userDetails);
		updateChatter.setDate(new Date());
		updateChatter.setFollowedId(accountId);
		updateChatter.setFollowType(FOLLOWTYPE.Account.getType());
		updateChatter.setUpdateType(UPDATETYPE.Followed.getName());
		
		Post post = new Post();
		post.setPostDate(new Date());
		post.setPoster(userDetails);
		post.setPostText("<a href = '/sales/s/viewAccount.html?id="+accountId+ "&b=c'>i followed Account Name:"+accountService.getAccount(accountId).getAccountName()+"</a>");
		postService.addPost(post);	
		updateChatter.setPost(post);
		updateChatterService.add(updateChatter);
		return "redirect:/s/accounts.html";
		}else{
			return "accessDenied";
		}
	}
	
	@RequestMapping(value="/followContact", method=RequestMethod.GET)
	public String postFollow(Model model,HttpServletRequest request,HttpServletResponse response,HttpSession session){
		if (SecurityUtils.getSubject().hasRole("contactFellow")){
		Employee userDetails = (Employee)session.getAttribute("userDetails");
		Integer contactId= Integer.parseInt(request.getParameter("contactId").toString());
//		FollowType followType = followTypeService.getFollowTypeById(2);
		FollowCenter followCenter = new FollowCenter();
		followCenter.setDate(new Date());
		followCenter.setFollowedType(FOLLOWTYPE.Contact.getType());
		followCenter.setFollower(userDetails);
		followCenter.setFollowedId(contactId);
		followCenterService.addfollowCenter(followCenter);
		//save post to view on chatter page
//		UpdateType updateType = updateTypeService.getById(3);
		UpdateChatter updateChatter = new UpdateChatter();
		updateChatter.setAction_emp(userDetails);
		updateChatter.setDate(new Date());
		updateChatter.setFollowedId(contactId);
		updateChatter.setFollowType(FOLLOWTYPE.Contact.getType());
		updateChatter.setUpdateType(UPDATETYPE.Followed.getName());
		
		Post post = new Post();
		post.setPostDate(new Date());
		post.setPoster(userDetails);
		post.setPostText("<a href = '/sales/s/viewContact.html?id="+contactId+ "&b=c'>i followed Contact Name:"+contactService.getContactById(contactId).getfName()+" "+contactService.getContactById(contactId).getlName()+ "</a>");
		postService.addPost(post);	
		updateChatter.setPost(post);
		updateChatterService.add(updateChatter);
		return "redirect:/s/contacts.html";
		}else{
			return "accessDenied";
		}
	}
	
	@RequestMapping(value="/followPeople", method=RequestMethod.GET)
	public String getFollowPeople(Model model,HttpServletRequest request,HttpServletResponse response,HttpSession session){
		Employee userDetails = (Employee)session.getAttribute("userDetails");
		Integer empId= Integer.parseInt(request.getParameter("empId").toString());
//		FollowType followType = followTypeService.getFollowTypeById(5);
		FollowCenter followCenter = new FollowCenter();
		followCenter.setDate(new Date());
		followCenter.setFollowedType(FOLLOWTYPE.People.getType());
		followCenter.setFollower(userDetails);
		followCenter.setFollowedId(empId);
		followCenterService.addfollowCenter(followCenter);
		//save post to view on chatter page
//		UpdateType updateType = updateTypeService.getById(3);
		UpdateChatter updateChatter = new UpdateChatter();
		updateChatter.setAction_emp(userDetails);
		updateChatter.setDate(new Date());
		updateChatter.setFollowedId(empId);
		updateChatter.setFollowType(FOLLOWTYPE.People.getType());
		updateChatter.setUpdateType(UPDATETYPE.Followed.getName());
		 Employee employee =  employeeService.loadEmployeeById(empId);
		Post post = new Post();
		post.setPostDate(new Date());
		post.setPoster(userDetails);
		post.setPostText("<a href = '/sales/s/viewPeople.html?id="+empId+ "&b=c'>"+userDetails.getfName()+" followed "+employee.getfName()+
				"</a>");
		postService.addPost(post);	
		updateChatter.setPost(post);
		updateChatterService.add(updateChatter);
		return "redirect:/s/people.html";
	}	
	

}
